package com.ciaojian.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciaojian.client.model.bo.ShareBO;
import com.ciaojian.core.model.ShareRecord;
import com.ciaojian.core.service.MarketingScanService;
import com.ciaojian.core.service.MarketingShareService;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author Atlakyn
 */
@Api(tags = "分享")
@RestController
@RequestMapping("/share")
public class ShareController {

    @Resource
    private MarketingShareService marketingShareService;

    @ApiOperation("用户分享次数")
    @ApiImplicitParams(@ApiImplicitParam(value = "userId", name = "用户id"))
    @GetMapping("/counter")
    public Result counterShare(@NotNull(message = "用户id不能为空") Integer userId) {
        LambdaQueryWrapper<ShareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareRecord::getInviteId, userId);
        queryWrapper.isNotNull(ShareRecord::getShareId);
        return Result.ok(new ShareRecord().selectCount(queryWrapper));
    }

    @ApiOperation("分享给朋友")
    @ApiImplicitParams(@ApiImplicitParam(dataTypeClass = ShareBO.class))
    @ApiResponses(@ApiResponse(code = 200, message = "success", response = ShareRecord.class))
    @PostMapping
    public Result share(@Validated @RequestBody ShareBO shareBO){
        ShareRecord shareRecord = new ShareRecord();
        BeanUtils.copyProperties(shareBO, shareRecord);
        shareRecord.insert();
        return Result.ok(shareRecord);
    }

    @ApiModelProperty("分享有礼-查询裂变活动")
    @GetMapping("/getById")
    public Result getById(Integer mId){
        return Result.ok( marketingShareService.getById(mId));
    }

    @ApiOperation("分享有礼-裂变活动")
    @GetMapping("/marketingShare")
    public Result marketingShare( @ApiParam(name = "inviterId", value = "用户id") Integer inviterId,
                                  @ApiParam(name ="shareId",value = "被分享者id")Integer shareId,
                                  @NotNull(message = "裂变活动id不能为空") @ApiParam(name = "mId", value = "裂变活动id") Integer mId){

        return marketingShareService.sendCoupon(inviterId,shareId,mId);
    }

    @ApiModelProperty("裂变活动-用户邀请多少人")
    @GetMapping("/marketingShareCount")
    public Result marketingShareCount(@ApiParam(name = "inviterId", value = "分享者id") Integer inviterId,
                                      @ApiParam(name ="shareId",value = "被分享者id")Integer shareId,
                                      @NotNull(message = "裂变活动id不能为空") @ApiParam(name = "mId", value = "裂变活动id") Integer mId){
        return Result.ok(marketingShareService.shareCount(inviterId,mId));
    }



}
